require('es6-promise').polyfill();
import  axios from  "axios"
import {getPath} from "../../assets/commonjs/env.js";

/**
 *
 * @param {*} type   是get请求还是post请求
 * @param {*} url    http请求
 * @param {*} params Object
 */

export function http(type,url,params){
  var  httpreq = getPath('api_path');   //开发环境  //http://192.168.3.17:80
  //var  httpreq = "http://192.168.3.44:7059/";
  return  new Promise((resolve,reject)=>{
    if(type.toLowerCase() == "file"){
      axios({
        method: 'post',
        url: httpreq + url,
        data: {},
        responseType:'blob',
      }).then(res => {
        resolve(res)
      })
    }else if(type.toLowerCase() == "sign"){
      axios.post(getPath('ca_path') + url,params).then(res=>{
        resolve(res)
      }).catch(err=>{
        reject(err)
      })
    }else if(type.toLowerCase() == "get"){
      axios.get(httpreq + url,{params}).then(res=>{
        resolve(res)
      }).catch(err=>{
        reject(err)
      })
    }else{
      axios.post(httpreq + url,params).then(res=>{
        resolve(res)
      }).catch(err=>{
        reject(err)
      })
    }
  })
}
